package com.stellar.circle.domain.sSchoolAdmin.impl;

import cn.hutool.core.util.ObjectUtil;
import com.stellar.auth.api.dto.UserInfoDTO;
import com.stellar.circle.basic.entity.SSchool;
import com.stellar.circle.basic.entity.SSchoolAdmin;
import com.stellar.circle.basic.service.SSchoolAdminService;
import com.stellar.circle.basic.service.SSchoolService;
import com.stellar.circle.controller.admin.sSchoolAdmin.vo.SSchoolAdminRequestPageVO;
import com.stellar.circle.controller.admin.sSchoolAdmin.vo.SSchoolAdminRequestVO;
import com.stellar.circle.controller.admin.sSchoolAdmin.vo.SSchoolAdminResponseVO;
import com.stellar.circle.convert.SSchoolAdminConvert;
import com.stellar.circle.domain.sSchoolAdmin.SSchoolAdminDomainService;

import com.stellar.circle.rpc.UserRPC;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学校管理员映射表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-17 18:48:52
 */
@Service
public class SSchoolAdminDomainServiceImpl implements SSchoolAdminDomainService {

    @Resource
    private SSchoolAdminService sSchoolAdminService;

    @Resource
    private SSchoolService sSchoolService;

    @Resource
    private UserRPC userRPC;

    @Override
    public PageResult<SSchoolAdminResponseVO> page(SSchoolAdminRequestPageVO requestPageVO) {
        PageResult<SSchoolAdminResponseVO> pageResult = new PageResult<>();
        SSchoolAdmin queryEntity = SSchoolAdminConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<SSchoolAdmin> entities = sSchoolAdminService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = sSchoolAdminService.count(queryEntity);
        List<SSchoolAdminResponseVO> vos = SSchoolAdminConvert.INSTANCE.convertEntityToRespList(entities);

        for (SSchoolAdminResponseVO vo : vos) {
            Long userId = vo.getAdminId();
            UserInfoDTO userInfoDTO = userRPC.queryUserInfoByUid(userId);
            if (ObjectUtil.isNull(userInfoDTO)) continue;
            vo.setNickName(userInfoDTO.getNickName());
            vo.setAvatar(userInfoDTO.getAvatar());
            // 设置学校信息
            SSchool sSchool = sSchoolService.queryById(vo.getSchoolId());
            vo.setSchoolInfo(sSchool.getSchoolName() + "-" + sSchool.getCampuses());
        }

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(SSchoolAdminRequestVO requestVO) {
        SSchoolAdmin entity = SSchoolAdminConvert.INSTANCE.convert(requestVO);
        return sSchoolAdminService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        SSchoolAdmin entity = new SSchoolAdmin();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return sSchoolAdminService.update(entity) != 0;
    }

    @Override
    public Boolean insert(SSchoolAdminRequestVO requestVO) {
        SSchoolAdmin entity = SSchoolAdminConvert.INSTANCE.convert(requestVO);
        return sSchoolAdminService.insert(entity) != 0;
    }

    @Override
    public SSchoolAdminResponseVO get(Long id) {
        SSchoolAdmin entity = sSchoolAdminService.queryById(id);
        return SSchoolAdminConvert.INSTANCE.convert(entity);
    }
}
